package com.ldzl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ldzl.dto.QueryInventoryNumDTO;
import com.ldzl.pojo.CkWarning;
import com.ldzl.service.CkStockDetailService;
import com.ldzl.service.CkWarningService;
import com.ldzl.mapper.CkWarningMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 风止
* @description 针对表【ck_warning(库存上下限表)】的数据库操作Service实现
* @createDate 2025-07-04 16:44:00
*/
@Service
public class CkWarningServiceImpl extends ServiceImpl<CkWarningMapper, CkWarning>
    implements CkWarningService{

    @Autowired
    private CkStockDetailService sds; //库存详情服务

    /**
     * 查询单个
     * @param product_code
     * @return
     */
    @Override
    public CkWarning selectWarning(String product_code) {
        CkWarning warning = super.baseMapper.selectOne(
                new QueryWrapper<CkWarning>()
                        .eq("product_code", product_code)
                        .eq("is_delete", 0)
        );
        //获取当前库存
        warning.setQueryNum(sds.queryStock(warning.getProduct_code()));
        return warning;
    }

    /**
     * 查询多个
     * @param listCode
     * @return
     */
    @Override
    public List<CkWarning> selectWarningList(List<String> listCode) {
        // 执行查询
        List<CkWarning> warningsList = this.list(new QueryWrapper<CkWarning>()
                .in("product_code", listCode)
                .eq("is_delete", 0));
        //获取多个当前库存
        List<QueryInventoryNumDTO> nums = sds.queryStockList(listCode);
        //遍历 赋值当前库存
        for (CkWarning warning : warningsList) {
            for (QueryInventoryNumDTO num : nums) {
                //给对应的编号赋值
                if (warning.getProduct_code().equals(num.getItem_code())) {
                    warning.setQueryNum(num);
                }
            }
        }
        return warningsList;
    }
}




